<?php
/**
 * Defines Kodform_Validator_Regex class
 *
 *
 * @copyright 	Copyright (c) 2006 Boris Tomic (kodmasin.net)
 * @category 	Kodform
 * @package 	Kodform
 * @author 		Boris Tomic
 * @license 	http://boris.kodmasin.net/kodform4:license The BSD License
 *
 */
 
require_once 'Kodform/Validator.php';
 
/**
 * this is regex validator
 * 
 * this is base validator for many validator
 * but it is most powefull one
 */
class Kodform_Validator_Regex extends Kodform_Validator{
	private $pattern;
	/**
	 * creates regex validator
	 * 
	 * @param string $patter rexeg patter to match
	 * @param string $error validation error
	 */
	public function __construct($pattern, $error){
		parent::__construct($error);
		$this->pattern = $pattern;
	}
	/**
	 * validation
	 * 
	 * it is required to note here that all value should match pattern
	 * 
	 * @returns boolean true if valid false if not
	 * 
	 * @see Kodform_Validator
	 */
	public function validate($value){
		$matched = null;
		//if value is empty string or null return true
		if($value == "" || $value === null)
			return true;
		$ret = preg_match($this->pattern, $value, $matched);
		if($ret == 1 && $matched[0] == $value)
			return true;
		return false;
	}
}
